<template>
	<view class="warp">
		<image src="../../static/images/login-bg.jpg" mode="heightFix" style="height:100%;position: fixed;"></image>
		<div class="contentBox">
			<image :src="imgUrl+'logo.png'" mode=""
				style="width:191rpx;height:220rpx;position: relative;z-index:2;"></image>
			<view class="desc">
				消防掌上宝模拟仿真系统
			</view>
			<view class="formBox">
				<view class="singleInput">
					<!-- <van-field @change="(e)=>changeInput(e,'org_no')" :value="formData.org_no"
						:left-icon="icon1"></van-field> -->
						<u-field  v-model="formData.org_no" 
								icon="home"
								>
						</u-field>
				</view>
				<view class="singleInput">
					<u-field  v-model="formData.account"
							icon="account"
							>
					</u-field>
					<!-- <van-field placeholder="请输入用户名" :value="formData.account" :left-icon="icon2"
						@change="(e)=>changeInput(e,'account')"></van-field> -->
				</view>
				<view class="singleInput">
					<u-field type="password" v-model="formData.pwd"
							icon="lock-open"
							>
					</u-field>
					<!-- <van-field type="password" :value="formData.pwd" :left-icon="icon3"
						@change="(e)=>changeInput(e,'pwd')"></van-field> -->
				</view>
			</view>
			<view class="btnArea">
				<u-button  :custom-style="{borderRadius: '100rpx',overflow:'hidden',marginBottom:'25rpx',background: '#c22322',color:'#fff'}"
					  @click="doLogin">立即登录</u-button>
				<u-button 
					v-if="broType=='weixin'"
					plain
					:hair-line= "false"
					:custom-style="{borderRadius: '100rpx',overflow:'hidden',color:'black',width:'100%',background:'transparent',overflow:'hidden'}"
					open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">快捷登录</u-button>
					<u-button
						v-else
						:hair-line= "false"
						:custom-style="{borderRadius: '100rpx',overflow:'hidden',color:'black',width:'100%',background:'transparent',overflow:'hidden'}"
						>快捷登录</u-button>
			</view>
		</div>
	</view>
</template>
<script>
	const app=getApp()
	const baseUrl = app.globalData.imgUrl
	import {
		managerLogin,
		getCode,
		wechatLogin,
		getConfig
	} from '@/globalApi/requestApi.js';
	import MD5 from '@/util/md5.js'
	export default {
		components: {
			// jyfParser
		},
		data() {
			return {
				icon1: require('@/static/images/icon/icon1.png'),
				icon2: require('@/static/images/icon/icon2.png'),
				icon3: require('@/static/images/icon/icon3.png'),
				formData: {
					// account: '13688881112',
					// pwd: '123456',
					// platform:'1',
					// org_no:123123,
					account: '',
					pwd: '',
					platform: '1',
					org_no: ''
				},
				imgUrl:baseUrl,
				code: '',
				configData:null,
				broType:''
			}
		},
		onLoad() {
			this.getConfigData()
			let that = this
			this.formData.device_id = uni.getDeviceInfo().deviceId
				// #ifdef MP-WEIXIN
				this.broType = 'weixin'
					uni.login({
						provider: 'univerify',
						success: (loginRes) => {
							
							this.code = loginRes.code
							// this.getConfigData()
							
  

						},
						complete: (error) => {
							console.log(error)
						}
					})
				// #endif
				//#ifdef APP-PLUS
				this.broType = 'app'
					// uni.login({
					// 	provider: 'univerify',
						
					// 	success: (loginRes) => {
					// 		uni.closeAuthView()
					// 		console.log(loginRes)
					// 		let accessToken = loginRes.authResult.access_token
					// 		let openId = loginRes.authResult.openid
					// 		uniCloud.callFunction({
					// 		  name: 'testFunction', // 你的云函数名称
					// 		  data: {
					// 		    'access_token': loginRes.authResult.access_token, // 客户端一键登录接口返回的access_token
					// 		    'openid': openId // 客户端一键登录接口返回的openid
					// 		  }
					// 		}).then(res => {
					// 			console.log(res)
					// 		  // res.result = {  
					// 		  //   code: '',
					// 		  //   message: ''
					// 		  // }
					// 		  // 登录成功，可以关闭一键登录授权界面了
					// 		}).catch(err=>{
					// 		  // 处理错误
					// 		})
							
					
					
					// 	},
					// 	complete: (error) => {
					// 		console.log(error)
					// 	}
					// })
				//#endif
		},
		onShow() {
			// console.log(MD5.str_md5('e10adc3949ba59abbe56e057f20f883e'))
		},
		onBackPress(e) {

		},
		methods: {
			async getConfigData(){
				let that = this
				try{
					let res =await getConfig()
						let data = res.data
						let newVer = data.version.split('.').join('')
						plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
						    let nowVer = widgetInfo.version
							if(Number(nowVer)<Number(newVer)){
								that.$showModal2({
								   	title:'APP有新版本，是否更新？',
									icon:'none', 
									confirmText: "确认", // 确认按钮的文字  
									showCancel: true, 
									
								}).then(()=>{
									uni.downloadFile({
										// 存放最新安装包的地址
										url: res.data.download_url,
										success: (downloadResult) => {  
											if (downloadResult.statusCode === 200) { 
												uni.hideLoading();
												plus.runtime.install(downloadResult.tempFilePath,{ 
													force: false
												}, function() {
													console.log('install success...');  
													plus.runtime.restart();  
												}, function(e) {  
													uni.hideLoading();
													console.error('install fail...');  
												});  
											}
										}  
									}); 
								})
							}
							
						}) 
				}catch(e){
					//TODO handle the exception
				}
				
				
				
			},
			getgetProviderCode(data, loginRes) {
				let that = this
				uni.request({
					url: 'https://api.weixin.qq.com/sns/jscode2session',
					data: {
						appid: data.appid, //小程序的appid
						secret: data.secret, //小程序的secret
						js_code: this.code, //调用uni.login()成功后获得的code码
						grant_type: 'authorization_code'
					},
					method: 'GET',
					success: function(res) {
						var openid = res.data.openid //获取用户的openid
						uni.setStorageSync('openId', res.data.openid)
						// that.queryUserInfo(data,loginRes)
					}
				})
			},
			changeInput(e, key) {
				// console.log(e.detail,key)
				this.formData[key] = e.detail
			},
			doLogin() {
				managerLogin({
					...this.formData,
					...{
						pwd: MD5.hex_md5(this.formData.pwd),
					}
				}).then(res => {

					if (res.code == 200) {
						uni.setStorageSync('user', res.data.userinfo)
						uni.setStorageSync('token', res.data.userinfo.token)
						uni.showToast({
							title: '登录成功！',
							type: 'success'
						})
						getCode({
							js_code: this.code
						}).then(res => {
							if(res.code==200){
								uni.setStorageSync('openId', res.data.openid)
							}
						})
						setTimeout(function() {
							uni.switchTab({
								url: '../tabbar/home'
							})
						}, 2000)

					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},

			getPhoneNumber(e) {
				console.log(e)
				let data = {...e.detail,...this.configData}
				this.queryUserInfo(data)
			},
			queryUserInfo(userProfile) {

				getCode({
					js_code: this.code
				}).then(res => {
					if (res.code == 200) {
						var sessionKey = res.data.session_key
						uni.setStorageSync('openId', res.data.openid)
						wechatLogin({
							sessionKey: sessionKey,
							iv: userProfile.iv,
							encryptData: userProfile.encryptedData,
							spread_uid:0,
						}).then(result => {
							uni.setStorageSync('user', result.data.userinfo)
							uni.setStorageSync('token', result.data.userinfo.token)
							uni.showToast({
								title: '登录成功！',
								type: 'success'
							})
							setTimeout(function() {
								uni.switchTab({
									url: '../tabbar/home'
								})
							}, 2000)
						})
					}
				})
			}


		}
	}
</script>
<style scoped>
	/deep/ .van-cell__left-icon-wrap {
		margin-right: 30rpx
	}
	/deep/ .u-field{
		background:#fff
	}
	/deep/ .u-field-inner {
		/* padding: 25rpx 45rpx !important; */
	}
	
	/deep/ .van-icon {
		height: 100%
	}
	/deep/ .u-field{
		border-radius:100rpx
	}
	/deep/ .u-label{
		flex: 0 !important;
		margin-right: 20rpx;
	}
</style>
<style lang="less">
	.warp {
		width: 100%;
		height: 100vh;
		background: yellow;

		// background-image:url('../../static/images/login-bg.png')
		.contentBox {
			padding: 120rpx 60rpx 40rpx;
			display: flex;
			align-items: center;
			justify-content: flex-start;
			flex-direction: column;

			.desc {
				font-size: 45rpx;
				color: #D92726;
			}

			.formBox {
				width: 100%;
				margin-top: 60rpx;

				.singleInput {
					width: 630rpx;
					height: 100rpx;
					overflow: hidden;
					margin-top: 30rpx;
				}
			}

			.btnArea {
				width: 630rpx;
				height: 100rpx;
				margin-top: 95rpx;
			}
		}
	}
</style>